package com.qf.team2.netdisc.service.impl;

import com.qf.team2.netdisc.common.pojo.entity.Document;
import com.qf.team2.netdisc.common.pojo.entity.User;
import com.qf.team2.netdisc.common.util.MD5Util;
import com.qf.team2.netdisc.dao.DocumentFolderMapper;
import com.qf.team2.netdisc.service.DocumentFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;

@Service
public class DocumentFolderServiceImpl implements DocumentFolderService {
    @Autowired
    private DocumentFolderMapper documentFolderMapper;

    @Override
    public Long checkUserCapacity(Integer id, MultipartFile[] file) {
        Long size = 0L;
        for (MultipartFile f : file) {
            Long filesize = f.getSize();
            System.out.println(filesize);
            if (filesize == 0) {
                filesize = 1L;
            }
            size = size + filesize;
            System.out.println(size);
        }
        if (file == null) {
            throw new RuntimeException("你没有选择文件，请重新选择");
        }
        System.out.println(size);
        User user = documentFolderMapper.selectDocumentById(id);
        if (user.getCapacity().equals(1) && user.getCapacity() + size > 1024 * 1024) {
            throw new RuntimeException("用户容量不住,请充值");
        }
        if (user.getCapacity().equals(2) && user.getCapacity() + size > 1024 * 1024 * 1024) {
            throw new RuntimeException("用户容量不住,请充值");
        }
        return size;
    }

    @Override
    public Integer folderUpload(MultipartFile[] file, Integer userId, String url, Long sizes) {
        for (MultipartFile f : file) {
            File file1;
            String name = "";
            try {
                if (f instanceof CommonsMultipartFile) {
                    //转换成这个对象，然后我们需要通过里面的FileItem来获得相对路径
                    CommonsMultipartFile f2 = (CommonsMultipartFile) f;
                    name = f2.getFileItem().getName();
                    System.out.println(name + "        ---------相对路径");
                    name = url + name;
                    file1 = new File("D://bf-netdisc" + "/" + name);
                    file1.mkdirs();
                    file1.createNewFile();
                    f.transferTo(file1);
                    long size = f.getSize() / 1024;
                    if (size == 0) {
                        size = 1L;
                    }
                    name = name.replace("/", "\\");
                    Integer b = fileFolder(name, file1, size, userId);
                }
                System.out.println(f.getOriginalFilename() + "   iii");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        documentFolderMapper.updateCapacityById(sizes, userId);
        return 1;
    }

    @Override
    public Integer fileFolder(String file, File files, Long size, Integer userId) {
        String folderName = file.replace("\\", "\\\\");
        String whileName = "\\\\" + file.replace("\\", "\\\\");
        System.out.println("whileName:" + whileName);
        System.out.println("folderName:" + folderName);
        String fileName = folderName.substring(folderName.lastIndexOf("\\") + 1);
        fileName = "\\\\" + fileName;
        String fileNames = folderName.substring(folderName.lastIndexOf("\\") + 1);

        System.out.println("fileName:" + fileName);
        int num = 1;
        while (!"".equals(whileName)) {
            System.out.println("fileNames:" + whileName);
            if (num == 1) {
                Document document = documentFolderMapper.selectDocumentByUrl(folderName, userId);
                if (document != null) {
                    throw new RuntimeException(fileNames + "此文件已存在");
                }
                String md5 = MD5Util.md5(files);
                System.out.println(md5);
                documentFolderMapper.insertFileByUrl(fileNames, size, userId, folderName, md5);
                num = 2;
                folderName = folderName.replace(fileNames, "");
                whileName = whileName.replace(fileName, "");
                whileName.trim();
                fileName = whileName.substring(whileName.lastIndexOf("\\") + 1);
                fileName = "\\\\" + fileName;
                fileNames = whileName.substring(whileName.lastIndexOf("\\") + 1);
                continue;
            }

            System.out.println("whileName:" + whileName);
            System.out.println(folderName);
            System.out.println("fileName:" + fileName);
            Document document = documentFolderMapper.selectDocumentByUrl(folderName, userId);
            if (document != null) {
                documentFolderMapper.upadteByUrl(size, folderName, userId);
                folderName = folderName.replace(fileName, "");
                whileName = whileName.replace(fileName, "");
                whileName.trim();
                fileName = whileName.substring(whileName.lastIndexOf("\\") + 1);
                fileName = "\\\\" + fileName;
                fileNames = whileName.substring(whileName.lastIndexOf("\\") + 1);
                continue;
            }
            if (document == null) {
                documentFolderMapper.insertFolderByUrl(fileNames, size, userId, folderName);
                folderName = folderName.replace(fileName, "");
                whileName = whileName.replace(fileName, "");
                whileName.trim();
                fileName = whileName.substring(whileName.lastIndexOf("\\") + 1);
                fileName = "\\\\" + fileName;
                fileNames = whileName.substring(whileName.lastIndexOf("\\") + 1);
                continue;
            }

        }
        return 1;
    }

}
